<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #box {
            width: 300px;
            height: 300px;
            border: 1px dashed red;
        }
    </style>
</head>

<body>
    <a href="#news">news</a>
    <a href="#messages">messages</a>
    <hr>
    <div id="box"></div>
    <script>
        //获取div元素
        let box = document.querySelector('#box');
        //onhashchange事件是在window对象中
        //只要地址栏中的hash字符串的值发生了改变的时候就会自动触发这个事件
        //DOM0级别：对象.on事件名 = function(){}
        //DOM2级别：对象.addEventListener('事件名',事件函数,是否捕获/冒泡)
        window.onhashchange = function () {
            //从地址对象中获取hash字符串部分
            let { hash } = location;
            //判断
            switch (hash) {
                case '#news':
                    box.innerHTML = '我是news组件的内容';
                    break;
                case '#messages':
                    box.innerHTML = '我是messages组件的内容';
                    break;
            }
        }
    </script>
</body>

</html>